package com.water.guider.highlight

import android.graphics.Canvas
import android.graphics.DashPathEffect
import android.graphics.Paint
import kotlin.math.hypot


/**
 * 圆形的高亮区域,外环是虚线的圆,并且和高亮区域的圆形是同心.
 */
class DashCircleHighlight : IHighlight {

    override var width = 0

    override var height = 0

    var padding = 0

    var radius = 0

    override var ringWidth = 0

    private val mDashPathEffect by lazy {
        DashPathEffect(floatArrayOf(15F, 15F), 0F)
    }

    constructor(ringWidth: Int = 0) {
        this.ringWidth = ringWidth
    }

    constructor(width: Int, height: Int, padding: Int = 0, ringWidth: Int = 0) {
        this.width = width
        this.height = height
        this.padding = padding
        this.ringWidth = ringWidth
        init()
    }

    override fun init() {
        radius = hypot(width.toDouble(), height.toDouble()).toInt() / 2 + padding
        width = radius * 2
        height = radius * 2
    }

    override fun draw(canvas: Canvas, paint: Paint, x: Int, y: Int) {
        canvas.drawCircle(x.toFloat(), y.toFloat(), radius.toFloat(), paint)
    }

    override fun draw(canvas: Canvas, paint: Paint, x: Int, y: Int, value: Int) {
        val circleRadius = if (value >= radius) radius.toFloat() else value.toFloat()
        val outRadius = (circleRadius + ringWidth)

        paint.style = Paint.Style.FILL
        canvas.drawCircle(x.toFloat(), y.toFloat(), circleRadius, paint)

        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5F
        paint.pathEffect = mDashPathEffect
        canvas.drawCircle(x.toFloat(), y.toFloat(), outRadius, paint)
    }

    override val max: Int
        get() = width.coerceAtLeast(height)
}